package com.cy.store.service.impl;

import com.cy.store.entity.User;
import com.cy.store.mapper.UserMapper;
import com.cy.store.service.IUserService;
import com.cy.store.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.UUID;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void reg(User user) {
        String username = user.getUsername();
        User result = userMapper.findByUsername(username);
        System.out.println(result);
        if(result != null){
            // 是：表示用户名已被占用，则抛出UsernameDuplicateException异常
            throw new UsernameDuplicateException("尝试注册的用户名[" + username + "]已经被占用");
        }
        String password = user.getPassword();
        String salt = UUID.randomUUID().toString().toLowerCase();
        user.setSalt(salt);
        String md5password = MD5Password(password, salt);
        user.setPassword(md5password);

        user.setIsDelete(0);
        user.setCreatedUser(user.getUsername());
        user.setModifiedUser(user.getUsername());
        Date date = new Date();
        user.setCreatedTime(date);
        user.setModifiedTime(date);


        Integer rows = userMapper.insert(user);
        if(rows != 1 ){
            throw  new InsertException("在注册过程中产生未知异常");
        }
    }

    @Override
    public User login(String username, String password) {
        User result = userMapper.findByUsername(username);
        if(result == null){
            throw new UserNotFoundException("用户数据不存在异常");
        }

        if(result.getIsDelete() == 1){
            throw  new UserNotFoundException("用户数据不存在错误");
        }
        String salt = result.getSalt();
        String md5Password = MD5Password(password, salt);
        if(!result.getPassword().equals(md5Password)){
            throw new PasswordNotMatchException("密码验证失败错误");
        }
        User user = new User();
        user.setUid(result.getUid());
        user.setUsername(result.getUsername());
        user.setAvatar(result.getAvatar());
        return user;

    }

    @Override
    public void changepassword(Integer uid, String username, String oldPassword, String newPassword) {
        User result = userMapper.findByUid(uid);
        if(result == null || result.getIsDelete()==1){
            throw  new UserNotFoundException("用户数据不存在");
        }
        /*原始密码与初始密码进行比较*/
        String md5Password = MD5Password(oldPassword, result.getSalt());
        if(!result.getPassword().equals(md5Password)){
            throw new PasswordNotMatchException("密码错误");
        }
        String newMDPassword = MD5Password(newPassword, result.getSalt());
        Integer rows = userMapper.updatePasswordByUid(uid, newMDPassword, username, new Date());
        if (rows != 1){
            throw new UpdateException("密码更新异常");
        }

    }


    private String MD5Password(String password,String salt){
        for (int i = 0; i <3; i++) {
             password = DigestUtils.md5DigestAsHex((salt + password + salt).getBytes()).toLowerCase();
        }
        return password;
    }
}
